<?php 
	session_start();
	require_once("Constantes.php");
	require_once("dao/Connection.php");
	require_once("dao/UserDAO.php");
	require_once("Traducteur.php");
	
	class CommonAction {
		private $trad;
	
		public function __construct() {
			$this->trad = new Traducteur("fr");
		}
		
		public function execute() {
			if (isset($_GET["lang"]) && strlen($_GET["lang"]) > 0){
				$_SESSION["lang"] = $_GET["lang"];
				$lang = $_GET["lang"];
			}
			else if (isset($_SESSION["lang"]) && strlen($_SESSION["lang"]) > 0){
				$lang = $_SESSION["lang"];
			}
			else
			{
				$_SESSION["lang"]="fr";
				$lang = "fr";
			}
			$this->trad = new Traducteur($lang);
			if (isset($_GET["logout"])){
				$this->logout();
			}
		}
		
		public function isLogged(){
			if (strcmp($_SESSION["login"], "logged")==0){
				return true;
			}
			else{
				return false;
			}
		}
		
		public function logout(){
			unset($_SESSION["login"]);
			header("Location:index.php");
		}
		
		public function lireNode($section, $node){
			return $this->trad->lireNode($section, $node);
		}
		
		public function lireBD($params, $table){
			$connection = Connection::connect();
			$query = "SELECT * FROM ".$table;
			
			$statement = oci_parse($connection, $query);
			oci_execute($statement);
			
			$data = array();
			$compteur=0;
			
			while ($row = oci_fetch_array($statement)) {

				$arrayLigne = array();
				
				foreach($params as $param){
					$arrayLigne[$param]=$row[$param];
				}
				
				$data[]= $arrayLigne;									
			}
			
			Connection::close($connection);
			
			return $data;
		}
		
		public function insertBD($colonnes, $valeurs, $table){			
		
			$connection = Connection::connect();
			$query = "INSERT INTO " . $table . " (";
			
			//colonnes
			for ($i=1; $i<sizeof($colonnes); $i++){
				$query .= $colonnes[$i] . ",";
			}
			//enlever la dernière virgule
			$query = substr($query, 0, -1);
			
			//values(:val1, :val2, etc)
			//on commence à un parce que le tableau colonnes¸
			//contient ID, qui sera affecté par un trigger
			$query .= ") values(";
			for ($i=1; $i<sizeof($colonnes); $i++){
				$query .= ":val".$i." ,";
			}
			$query = substr($query, 0, -1);
			$query .= ")";
			
			$statement = oci_parse($connection, $query);
			
			//on affecte donc à valX la valeur $valeurs[X-1]
			//parce qu'on a pas envoyé l'ID
			for ($i=1; $i<sizeof($colonnes); $i++){
				$param = ":val".$i;
				oci_bind_by_name($statement, $param, $valeurs[$i-1]);
			}
			
			oci_execute($statement);
			
			Connection::close($connection);
			
		}
		
		public function updateBD($id, $colonnes, $valeurs, $table){
			$connection = Connection::connect();
			
			$query = "UPDATE " . $table . " SET ";
			
			//ajouter les valeurs à binder
			for ($i=1; $i<sizeof($colonnes); $i++){
				$query .= $colonnes[$i] . "= :val".$i." ,";
			}
			//enlever la dernière virgule
			$query = substr($query, 0, -1);
			
			$query .= " WHERE ID = ".(string)$id;
			$statement = oci_parse($connection, $query);			
			//binder colonnes et valeurs

			for ($i=1; $i<sizeof($colonnes); $i++){
				$param = ":val".$i;
				oci_bind_by_name($statement, $param, $valeurs[$i]);
			}

			oci_execute($statement);
			
			Connection::close($connection);
		}
		
		public function deleteBD($id, $table){
			$connection = Connection::connect();
			
			$query = "DELETE FROM ".$table." WHERE ID = :val1";
			
			$statement = oci_parse($connection, $query);
			
			oci_bind_by_name($statement, ":val1", $id);
			
			oci_execute($statement);
			
			Connection::close($connection);
		}
		
	}